﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.IO;

namespace MyVoteMVC.Models
{
    public class Ticket
    {
        /// <summary>
        /// PK Guid
        /// </summary>
        public string ID { get; set; }

        /// <summary>
        /// FK 所属活动ID
        /// </summary>
        public string Activity_ID { get; set; }

        /// <summary>
        /// FK 提交用户ID
        /// </summary>
        public string User_ID { get; set; }

        /// <summary>
        /// 票据号
        /// </summary>
        public int? No { get; set; }

        /// <summary>
        /// 票据状态
        /// </summary>
        public int? Status { get; set; }

        /// <summary>
        /// 票据提交时间
        /// </summary>
        public DateTime? SubmitTime { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public string Created_By { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? Created_Date { get; set; }

        /// <summary>
        /// 创建方法
        /// </summary>
        public string Created_Action { get; set; }

        /// <summary>
        /// 更新人
        /// </summary>
        public string Last_Update_By { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? Last_Update_Date { get; set; }

        /// <summary>
        /// 更新方法
        /// </summary>
        public string Last_Update_Action { get; set; }

        //LazyLoad

        /// <summary>
        /// 票据相关的活动
        /// </summary>
        public virtual Activity Activity { get; set; }

        /// <summary>
        /// 票据相关的用户
        /// </summary>
        public virtual User User { get; set; }

        public ContentDetailContainer ContentDetail { get; set; }
        /// <summary>
        /// 票据实体
        /// </summary>
        public string Content
        {
            get
            {
                return ContentDetail.ToString();
            }
            set
            {
                ContentDetail = new ContentDetailContainer(value);
            }
        }

        public class ContentDetailContainer
        {
            public ActivityInContent Activity { set; get; }

            public ContentDetailContainer()
            {
            }

            public ContentDetailContainer(string content)
            {
                try
                {
                    var serializer = new DataContractJsonSerializer(typeof(ActivityInContent));
                    var mStream = new MemoryStream(Encoding.Default.GetBytes(content));
                    Activity = (ActivityInContent)serializer.ReadObject(mStream);
                }
                catch
                {
                    throw;
                }
            }

            public override string ToString()
            {
                try
                {
                    var serializer = new DataContractJsonSerializer(typeof(ActivityInContent));
                    var stream = new MemoryStream();
                    serializer.WriteObject(stream, Activity);
                    byte[] dataBytes = new byte[stream.Length];
                    stream.Position = 0;
                    stream.Read(dataBytes, 0, (int)stream.Length);

                    string dataString = Encoding.UTF8.GetString(dataBytes);

                    return dataString;
                }
                catch
                {
                    throw;
                }
            }
        }
    }

    [DataContract]
    public class ActivityInContent
    {
        [DataMember]
        public string Id { set; get; }

        [DataMember]
        public IList<QuestionInContent> Questions { set; get; }

        public ActivityInContent()
        {
            Questions = new List<QuestionInContent>();
        }
    }

    [DataContract]
    public class QuestionInContent
    {
        [DataMember]
        public string Id { set; get; }

        [DataMember]
        public IList<ItemInContent> Items { set; get; }

        public QuestionInContent()
        {
            Items = new List<ItemInContent>();
        }
    }

    [DataContract]
    public class ItemInContent
    {
        [DataMember]
        public string Id { set; get; }
    }
}
